<%@ page language="java" import="weaver.conn.RecordSet" pageEncoding="UTF-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%
    BaseBean baseBean = new BaseBean();
    RecordSet rs1 = new RecordSet();
    baseBean.writeLog( "-----------WeaPurchasePlanCronJob------------" );
    rs.executeQuery( "select n.zcdbjcrq,n.id,n.xqrq,m.dbjcsprq,m.requestid,m.zgshwcrq,m.zbwjfbrq,m.tbjzrq,m.slkbrq,m.pbjghqrq,m.zbtzrq \n" +
            "from uf_cgxq_wl n left join\n" +
            "(select a.id,a.xqdid,b.dbjcsprq,b.requestid,b.zgshwcrq,b.zbwjfbrq,b.tbjzrq,b.slkbrq,b.pbjghqrq,b.zbtzrq \n" +
            "from uf_cgrw_wl a,formtable_main_1033 b where EXISTS (SELECT COLUMN_VALUE\n" +
            "FROM TABLE(SPLITSTR(b.rwdids, ',')) c WHERE c.column_value = a.id))m\n" +
            "on n.id=m.xqdid where n.zcdbjcrq is not null or m.dbjcsprq is not null" );
    int counts = rs.getCounts();
    out.print( "--------------------counts=" + counts + "<br/>" );
    int count = 0;
    while (rs.next()) {
        String zcdbjcrq = Util.null2String( rs.getString( "zcdbjcrq" ) );//最迟定标决策日期
        String id = Util.null2String( rs.getString( "id" ) );
        String xqrq = Util.null2String( rs.getString( "xqrq" ) );//需求创建日期
        String dbjcsprq = Util.null2String( rs.getString( "dbjcsprq" ) );//定标决策审批日期
        String requestid = Util.null2String( rs.getString( "requestid" ) );
        String zgshwcrq = Util.null2String( rs.getString( "zgshwcrq" ) );//资格审核完成日期
        String zbwjfbrq = Util.null2String( rs.getString( "zbwjfbrq" ) );//招标文件发布日期
        String tbjzrq = Util.null2String( rs.getString( "tbjzrq" ) );//投标截止日期
        String slkbrq = Util.null2String( rs.getString( "slkbrq" ) );//首轮开标日期
        String pbjghqrq = Util.null2String( rs.getString( "pbjghqrq" ) );//评标结果会签日期
        String zbtzrq = Util.null2String( rs.getString( "zbtzrq" ) );//定标决策审批日期

        if ("".equals( dbjcsprq )) {
            long syzbzq = getDays( zcdbjcrq );
            long pcrq = getDays( xqrq ) + 2;
            if (syzbzq <= 0) {
                rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=0 ,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
            } else if (syzbzq <= 7) {
                rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=0 ,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
            } else if (syzbzq > 7) {
                rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=0 ,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
            }
        } else {
            long syzbzq = getDays( dbjcsprq );
            //招标采购立项
            rs1.executeQuery( "select count(n.requestid) as count1 from formtable_main_1033 n\n" +
                    "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.requestid =?", requestid );
            rs1.next();
            int count1 = Util.getIntValue( rs1.getString( "count1" ) );
            out.print( "--------------------count1=" + count1 + "<br/>" );
            if (count1 > 0) {
                long pcrq = getDays( xqrq ) + 4;
                if (syzbzq <= 0) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=1,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                } else if (syzbzq <= 7) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=1,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                } else if (syzbzq > 7) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=1,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                }
            }
            //供应商资格预审
            rs1.executeQuery( "select count(n.requestid) as count2 from formtable_main_1038 n\n" +
                    "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =? ", requestid );
            rs1.next();
            int count2 = Util.getIntValue( rs1.getString( "count2" ) );
            out.print( "--------------------count2=" + count2 + "<br/>" );
            if (count2 > 0) {
                long pcrq = getDays( zgshwcrq );
                if (syzbzq <= 0) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=2,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                } else if (syzbzq <= 7) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=2,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                } else if (syzbzq > 7) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=2,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                }
            }
            //发起招标
            rs1.executeQuery( "select count(n.requestid) as count3 from formtable_main_1042 n\n" +
                    "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
            rs1.next();
            int count3 = Util.getIntValue( rs1.getString( "count3" ) );
            out.print( "--------------------count3=" + count3 + "<br/>" );
            if (count3 > 0) {
                long pcrq = getDays( zbwjfbrq );
                if (syzbzq <= 0) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=3,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                } else if (syzbzq <= 7) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=3,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                } else if (syzbzq > 7) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=3,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                }
            }
            //供应商投标
            rs1.executeQuery( "select count(n.requestid) as count4 from formtable_main_1043 n\n" +
                    "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
            rs1.next();
            int count4 = Util.getIntValue( rs1.getString( "count4" ) );
            out.print( "--------------------count4=" + count4 + "<br/>" );
            if (count4 > 0) {
                long pcrq = getDays( tbjzrq );
                if (syzbzq <= 0) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=4,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                } else if (syzbzq <= 5) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=4,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                } else if (syzbzq > 5) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=4,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                }
            }
            //开标结果会签
            rs1.executeQuery( "select count(n.requestid) as count5 from formtable_main_1046 n\n" +
                    "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
            rs1.next();
            int count5 = Util.getIntValue( rs1.getString( "count5" ) );
            out.print( "--------------------count5=" + count5 + "<br/>" );
            if (count5 > 0) {
                long pcrq = getDays( slkbrq );
                if (syzbzq <= 0) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=5,t.pcsj=? where t.id=?", syzbzq,pcrq,id );
                } else if (syzbzq <= 5) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=5,t.pcsj=? where t.id=?", syzbzq,pcrq,id );
                } else if (syzbzq > 5) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=5,t.pcsj=? where t.id=?", syzbzq,pcrq,id );
                }
            }
            //评标结果会签
            rs1.executeQuery( "select count(n.requestid) as count6 from formtable_main_1049 n\n" +
                    "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
            rs1.next();
            int count6 = Util.getIntValue( rs1.getString( "count6" ) );
            out.print( "--------------------count6=" + count6 + "<br/>" );
            if (count6 > 0) {
                long pcrq = getDays( pbjghqrq );
                if (syzbzq <= 0) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=6,t.pcsj=? where t.id=?", syzbzq,pcrq, id );
                } else if (syzbzq <= 2) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=6,t.pcsj=? where t.id=?", syzbzq,pcrq, id );
                } else if (syzbzq > 2) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=6,t.pcsj=? where id=?", syzbzq,pcrq, id );
                }
            }
            //供应商发起决策
            rs1.executeQuery( "select count(n.requestid) as count7 from formtable_main_1050 n\n" +
                    "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
            rs1.next();
            int count7 = Util.getIntValue( rs1.getString( "count7" ) );
            out.print( "--------------------count7=" + count7 + "<br/>" );
            if (count7 > 0) {
                long pcrq = getDays( dbjcsprq );
                if (syzbzq <= 0) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=7,t.pcsj=? where t.id=?", syzbzq,pcrq,id );
                } else if (syzbzq <= 2) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=7,t.pcsj=? where t.id=?", syzbzq,pcrq,id );
                } else if (syzbzq > 2) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=7,t.pcsj=? where t.id=?", syzbzq,pcrq,id );
                }
            }
            //中标通知
            rs1.executeQuery( "select count(n.requestid) as count8 from formtable_main_1052 n\n" +
                    "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
            rs1.next();
            int count8 = Util.getIntValue( rs1.getString( "count8" ) );
            out.print( "--------------------count8=" + count8 + "<br/>" );
            if (count8 > 0) {
                long pcrq = getDays( zbtzrq );
                if (syzbzq <= 0) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=8,t.pcsj=? where t.id=?", syzbzq,pcrq,id );
                } else if (syzbzq <= 7) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=8,t.pcsj=? where t.id=?", syzbzq,pcrq,id );
                } else if (syzbzq > 7) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=8,t.pcsj=? where t.id=?", syzbzq,pcrq,id );
                }
            }
        }
        count++;
        out.print( "----------------------------------count=" + count + "<br/>" );
    }
    out.print( "--------------------counts=" + counts );
    out.print( "--------------------count=" + count );
%>
<%!
    public static long getDays(String date) {
        long days = 0L;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
            Date d1 = sdf.parse( date );
            long ts1 = d1.getTime();
            Date d2 = sdf.parse( DateUtil.getCurrentDate() );
            long ts2 = d2.getTime();
            days = (ts1 - ts2) / (1000 * 60 * 60 * 24);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }
%>